from fastapi import APIRouter, Request
from app.api.image.hbimage import HBImageAPI
from app.api.image.jiugong import JiugongAPI
from app.api.image.siteImage import SiteImageAPI
from app.api.image.yanzhengma import YanZhengMaAPI
from app.api.image.koutu import KouTuAPI
from app.api.image.resize import ImageResizeAPI
from app.api.image.tosvg import ToSvgAPI
from app.api.image.transformer import ImageTransformerAPI
from fastapi import UploadFile, File, Form

router = APIRouter(prefix='/image')

@router.get('/heibai')
def heibai(url: str, type: str = "json"):
    return HBImageAPI().handle(url, type)

@router.get('/jiugong')
def jiugong(url: str, type: str = "json"):
    return JiugongAPI().handle(url, type)

@router.get('/site-images')
def site_images(url: str):
    return SiteImageAPI().handle(url)

# 在现有路由下添加
@router.get('/yanzhengma')
def yanzhengma(text: str, type: str = "json"):
    return YanZhengMaAPI().handle(text, type)

@router.get('/koutu')
def koutu(url: str, type: str = "json", bg_color: str = None):
    return KouTuAPI().handle(url, type, bg_color)

@router.post("/koutu")
async def koutu_upload(file: UploadFile = File(...), type: str = Form("json"), bg_color: str = Form(None)):
    return await KouTuAPI().handle_upload(file, type, bg_color)

@router.get('/resize')
def image_resize(url: str, width: int, height: int, type: str = "json"):
    return ImageResizeAPI().handle(url, width=width, height=height, type=type)

@router.post('/resize')
async def image_resize_upload(file: UploadFile = File(...), width: int = Form(...), height: int = Form(...), type: str = Form("json")):
    return await ImageResizeAPI().handle_upload(file, width=width, height=height, type=type)

@router.get('/tosvg')
def to_svg(url: str, type: str = "json"):
    return ToSvgAPI().handle(url, type)

@router.post('/tosvg')
async def to_svg_upload(file: UploadFile = File(...), type: str = Form("json")):
    return await ToSvgAPI().handle_upload(file, type)

# GET请求路由
@router.get("/format")
async def transform_image_get(request: Request, format: str = "png", type: str = "json", url: str = None):
    return await ImageTransformerAPI().handle(request, format, type, url)

# POST请求路由
@router.post("/format")
async def transform_image_post(
    request: Request, 
    format: str = Form("png"), 
    type: str = Form("json"),
    file: UploadFile = File(...)
):
    return await ImageTransformerAPI().handle(request, format, type, file=file)